/**
 * 
 */
package jvs.tea.html;


import java.net.URI;
import java.text.MessageFormat;

import jvs.vfs.util.note.Note;
import jvs.vfs.util.note.NoteHelper;
import jvs.vfs.util.note.NoteIterator;
import x.java.io.File;

/**
 * @author qiangli
 * 
 */
public class CommentTemplateInputStream extends FileObjectInputStream {
	private static final String TPL = Constant.TEMPLATE_DIR+"comment.htmf";
	private static final String COMMENT_TPL = "<hr class=\"dotline\" /><span class=\"comment\"><span class=\"comment-date\">{0}</span><br /><span class=\"comment-text\">{1}</span></span>";
	//private static final String COMMENT_TPL = "<hr class=\"dotline\" /><span class=\"comment\">&nbsp;&nbsp;<a href=\"/~/{1}\" target=\"_blank\">{2}</a>&nbsp;<span class=\"comment-date\">{0}</span><br /><span class=\"comment-text\">{3}</span></span>";

	private static final String HTML_BR = "<br />";
	public CommentTemplateInputStream(URI context, URI asset, File.Info file) {
		super(context, asset, file);
		init();
	}

	private void init() {
		try {
			String p = file.getPath();
			Object[] sa = new String[4];
			NoteIterator it = NoteHelper.notes(p, HTML_BR);
			StringBuilder sb = new StringBuilder();
			int cnt = 0;
			while (it.hasNext()) {
				Note n = it.next();
				sa[0] = n.date;
				sa[1] = n.body;
				//TODO reinstate if remote post is supported
				//sa[1] = n.author;
				//sa[2] = n.name;
				//sa[3] = n.body;
				String s = MessageFormat.format(COMMENT_TPL, sa);
				sb.append(s);
				cnt++;
			}
			addInjector("comments",sb.toString());
			addInjector("count", cnt+"");
		} catch (Exception e) {
			e.printStackTrace();
		}
		setInputStream(getClass().getResourceAsStream(TPL));
	}
}
